import Vue from 'vue'
import Vuex from 'vuex'
import createState from 'vuex-persistedstate'
import { getUserAPI } from '@/api/user.js'

Vue.use(Vuex)

export default new Vuex.Store({
  // 严格模式true：开启false：关闭
  // production:发布模式
  // development:开发模式
  strict: process.env.NODE_ENV === 'development',
  plugins: [createState()],
  state: {
    token: '',
    user: {}
  },
  mutations: {

    updateToken (state, token) {
      state.token = token
    },

    updateUser (state, obj) {
      state.user = obj
    }

  },
  actions: {

    async initUser (ctx) {
      const { data: res } = await getUserAPI()
      if (res.code === 0) {
        ctx.commit('updateUser', res.data)
      }
    }
  },
  modules: {
  }
})
